﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
<link href="~/lib/layui/css/layui.css" rel="stylesheet" />
<script src="~/lib/layui/layui.js"></script>
<script src="~/lib/jquery/dist/jquery.js"></script>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>IMES</title>
</head>
<body>
    <div class="layui-panel">
        <div style="padding: 32px;">
            <div class="layui-form-item">
                <form class="layui-form" action="">
                    <div class="layui-form-item">
                        <label class="layui-form-label">入库单号:</label>
                        <div class="layui-input-block">
                            <input type="text" Id="InboundOrderNumber" placeholder="请输入，忽略将自动生成" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">入库类型:</label>
                        <div class="layui-input-block">
                            <select name="InboundOrderTypeId" id="InboundOrderTypeId" lay-filter="aihao" style="width: 400px">
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">入库时间:</label>
                        <div class="layui-input-block">

                            <input type="datetime-local" id="InboundOrderDate" placeholder="开始时间" autocomplete="off" class="layui-input" name="dateStart">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">备注:</label>
                        <div class="layui-input-block">
                            <textarea Id="remark" type="text" placeholder="请输入备注" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <div class="layui-panel" style="margin-top: 20px;">
            <div style="padding: 32px;">
                <div class="layui-form-item">
                    <div class="layui-form-label">产品明细</div>
                    <div style="margin-left:800px" class="layui-inline">
                        <a class=" layui-btn-sm" lay-on="test-page-custom">+选择产品</a>
                        <a class=" layui-btn-sm">删除行</a>
                        <a class=" layui-btn-sm">导入</a>
                        <a class=" layui-btn-sm">刷新</a>
                    </div>
                </div>

                <table class="layui-table">
                    <thead>
                        <tr>
                            <th>
                                <input type="checkbox" name="check" />
                            </th>
                            <th>产品ID</th>
                            <th>产品编号</th>
                            <th>产品名称</th>
                            <th>库存单位</th>
                            <th>产品规格</th>
                            <th>最大库存</th>
                            <th>最小库存</th>
                            <th>安全库存</th>
                            <th>入库数量</th>
                            <th>当前库存数量</th>
                            <th>创建时间</th>
                            <th>创建人</th>
                            <th>修改人</th>
                            <th>修改时间</th>
                        </tr>
                    </thead>
                    <tbody id="body">
                    </tbody>
                </table>
                <div>
                    @* 红色保存按钮 *@
                    <button class="layui-btn layui-btn-danger" lay-submit lay-filter="add" onclick="add1()">保存</button>
                    <button type="reset" class="layui-btn layui-btn-primary">关闭</button>
                </div>
            </div>
        </div>
    </div>
    <!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
    <script src="//unpkg.com/layui@2.9.20/dist/layui.js"></script>
    <script>

    </script>
</body>
</html>

<!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
<script src="//unpkg.com/layui@2.9.20/dist/layui.js"></script>
<script>
    //弹窗产品选择
    layui.use(function () {
        var $ = layui.$;
        var layer = layui.layer;
        var util = layui.util;
        var form = layui.form;
        // 事件
        util.on('lay-on', {
            'test-page-custom': function () {
                layer.open({
                    type: 2,
                    area: ['800px', '800px'],
                    shadeClose: true,
                    title: '选择产品',
                    content: '/InboundOrder/InboundProductView',

                });
            }
        });

       
    });

</script>
<script>

    //获取产品列表选择的产品id
    var check = localStorage.getItem("check")
    //从地址栏获取入库单id
    var id = location.search.split("=")[1]
    



    $(function () {
        if(check != null){
 getProductCheck()
        }
       
        getInboundOrderType();
        //反添信息
        InboudOrderDetail()
    })

    //获取入库类型
    function getInboundOrderType() {
      $.ajax({
            url: '@ViewBag.GlennQuigmrire' + "/api/InboundOrder/InboundOrderTypeSearch",
            type: "get",
        dataType: "json",
        contentType: "application/json",
        success: function (res) {
            console.log(res)
            console.log(id)
            $("#InboundOrderTypeId").empty();
            $("#InboundOrderTypeId").append(`<option value="0">请选择</option>`)
            $(res.data).each(function () {
               $("#InboundOrderTypeId").append(`<option value="${this.inboundOrderTypeId}">${this.inboundOrderTypeName}</option>`)
                })
            layui.form.render('select');
        }
      })
    }

    //反添入库单信息
    function InboudOrderDetail() {
        console.log(id)
        $.ajax({
            url: '@ViewBag.GlennQuigmrire' + "/api/InboundOrder/InoutboundOrderDetail",
            type: "post",
            data: JSON.stringify({ inboundOrderId: id }),
            dataType: "json",
            contentType: "application/json",
            success: function (res) {
                console.log(res)
                //给表单赋值
                $("#InboundOrderNumber").val(res.item.inboundOrderNumber)
                $("#InboundOrderTypeId").val(res.item.inboundOrderTypeId)
                $("#InboundOrderDate").val(res.item.inboundOrderDate)
                $("#remark").val(res.item.remark)
                //刷新下拉框
                layui.form.render('select');
                //给产品表赋值
                $(res.item.inboundOrderProducts).each(function () {

                    var colors1 = "";
                    if (this.stockUnit == 1) {
                        colors1 = "red";
                    } else if (this.stockUnit == 2) {
                        colors1 = "blue";
                    } else if (this.stockUnit == 3) {
                        colors1 = "green";
                    } else if (this.stockUnit == 4) {
                        colors1 = "pink";
                    } else if (this.stockUnit == 5) {
                        colors1 = "blue";
                    } else if (this.stockUnit == 6) {
                        colors1 = "orange";
                    } else if (this.stockUnit == 7) {
                        colors1 = "gray";
                    } else if (this.stockUnit == 8) {
                        colors1 = "black";
                    } else if (this.stockUnit == 9) {
                        colors1 = "pink";
                    }
                    $("#body").append(`<tr>
                                                          <td><input type="checkbox" name="check"></input></td>
                                                           <td>${this.productId}</td>
                                                           <td>${this.productNumber}</td>
                                                           <td>${this.productName}</td>
                                                           <td>$ <th style="color:${colors1}" >${this.stockUnit == 1 ? "条" : (this.stockUnit == 2 ? "张" : (this.stockUnit == 3 ? "车" : (this.stockUnit == 4 ? "车" : (this.stockUnit == 5 ? "天" : (this.stockUnit == 6 ? "小时" : (this.stockUnit == 7 ? "分钟" : (this.stockUnit == 8 ? "周" : (this.stockUnit == 9 ? "月" : ""))))))))}</th></td>
                                                          <td>${this.maxInventory}</td>
                                                          <td>${this.minInventory}</td>
                                                          <td>${this.saveInventory}</td>
                                                          <td>
                                                                <input disabled type="text" style="width: 100px" value="${this.inboundOrderNum}" autocomplete="off" class="layui-input">
                                                              </td>
                                                          <td>${this.inventoryStore}</td>
                                                          <td>${this.createDate}</td>
                                                          <td>${this.createName}</td>
                                                          <td>${this.updateName == null ? "" : this.updateName}</td>
                                                          <td>${this.updateDate == null ? "" : this.updateDate}</td>
                                                      </tr>`)
                })
            }
        })
    }

    // 获取产品明细
    function getProductCheck() {
        $.ajax({
            url: '@ViewBag.GlennQuigmrire' + "/api/InboundOrder/GetInboundOrderProductDetail",
            type: "post",
            data: JSON.stringify({ ProductIds: check }),
            dataType: "json",
            contentType: "application/json",
            success: function (res) {
                console.log(res)
                $(res.data).each(function (item) {
                    
                    var colors1 = "";
                    if (this.stockUnit == 1) {
                        colors1 = "red";
                    } else if (this.stockUnit == 2) {
                        colors1 = "blue";
                    } else if (this.stockUnit == 3) {
                        colors1 = "green";
                    } else if (this.stockUnit == 4) {
                        colors1 = "pink";
                    } else if (this.stockUnit == 5) {
                        colors1 = "blue";
                    } else if (this.stockUnit == 6) {
                        colors1 = "orange";
                    } else if (this.stockUnit == 7) {
                        colors1 = "gray";
                    } else if (this.stockUnit == 8) {
                        colors1 = "black";
                    } else if (this.stockUnit == 9) {
                        colors1 = "pink";
                    }
                    $("#body").append(`<tr>
                                                          <td><input type="checkbox" name="check"></input></td>
                                                           <td>${this.productId}</td>
                                                           <td>${this.productNumber}</td>
                                                           <td>${this.productName}</td>
                                                           <td>$ <th style="color:${colors1}" >${this.stockUnit == 1 ? "条" : (this.stockUnit == 2 ? "张" : (this.stockUnit == 3 ? "车" : (this.stockUnit == 4 ? "车" : (this.stockUnit == 5 ? "天" : (this.stockUnit == 6 ? "小时" : (this.stockUnit == 7 ? "分钟" : (this.stockUnit == 8 ? "周" : (this.stockUnit == 9 ? "月" : ""))))))))}</th></td>
                                                          <td>${this.maxInventory}</td>
                                                          <td>${this.minInventory}</td>
                                                          <td>${this.saveInventory}</td>
                                                          <td>
                                                            <input type="text" style="width: 100px" value="1" autocomplete="off" class="layui-input">
                                                          </td>
                                                          <td>${this.inventoryStore}</td>
                                                          <td>${this.createDate}</td>
                                                          <td>${this.createName}</td>
                                                          <td>${this.updateName==null?"":this.updateName }</td>
                                                          <td>${this.updateDate==null?"":this.updateDate}</td>
                                                      </tr>`)
                })
            }
        })
        //取消记录的产品
        localStorage.removeItem("check")
    }


    //修改方法（添加）
    function add1() {
        var InboundOrderNumber = $("#InboundOrderNumber").val()||""
        var InboundOrderTypeId = $("#InboundOrderTypeId").val()
        var InboundOrderDate = $("#InboundOrderDate").val()
        var remark = $("#remark").val()
        var updateId = 1
        var updateName = "雷鑫";

        if (InboundOrderNumber == "") {
            var year = new Date().getFullYear();
            var month = (new Date().getMonth() + 1) < 10 ? "0" + (new Date().getMonth() + 1) : (new Date().getMonth() + 1);
            var day = new Date().getDate() < 10 ? "0" + new Date().getDate() : new Date().getDate();
            var hours = new Date().getHours() < 10 ? "0" + new Date().getHours() : new Date().getHours();
            var minutes = new Date().getMinutes() < 10 ? "0" + new Date().getMinutes() : new Date().getMinutes();
            var seconds = new Date().getSeconds() < 10 ? "0" + new Date().getSeconds() : new Date().getSeconds();

            //从1-3随机取一个数字
            var randomNum = Math.floor(Math.random() * 3) + 1;
            console.log(randomNum)
            //从0001到0003循环的序列号
            var str = "000" + randomNum;  // 前面补0，使总长度为4位
            console.log(str)
            //拼接订单号
            inboundOrderNumber = "RKD" + year + month + day + hours + minutes + seconds + str;

        }
        var data = {
            inboundOrderId: id,
            inboundOrderNumber: InboundOrderNumber,
            inboundOrderTypeId: InboundOrderTypeId,
            inboundOrderDate: InboundOrderDate,
            remark: remark,
            updateId: updateId,
            updateName: updateName,
        }
       
        console.log(data)
        $.ajax({
            url: '@ViewBag.GlennQuigmrire' + "/api/InboundOrder/InboundOrderUpdate",
            type: "post",
            async: false,
            data: JSON.stringify(data),
            dataType: "json",
            contentType: "application/json",
            success: function (res) {
                console.log(res)
                if (res.code > 0) {
                    layer.msg("保存成功", function () {
                         // 关闭当前弹出层
                         var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                         parent.layer.close(index); //再执行关闭
                         //刷新父页面
                         parent.location.reload();
                    });
                } else {
                    layer.msg(res.message);
                }
            }
        })
    }



</script>